
package com.innovamed.websocket.utils.time;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalTime;
import java.util.Date;
import java.util.Locale;


/***
* 
* Copyright: Copyright (c) 2019 Jun_Zhou
* 
* @ClassName: TimeUtils.java
* @Description: 时间解析相关的工具类;
* 
* @version: v1.0.0
* @author: JunZhou
* @Email: 1769676159@qq.com
* @Site: CERNO
* @date: 2019年7月15日 下午12:38:07
 */
public class TimeUtils
{
	/**
	 * 特殊格式时间解析方法;
	 *  eg:2019-07-11T16:00:00.000+0000
	 * @param oldDate : 老的数据格式, eg:2019-07-11T16:00:00.000+0000;
	 * @param targetFormat :目标数据格式，eg:"yyyy-MM-dd HH:mm:ss";
	 * @return :格式化后的日期字符串;
	 */
	public static String dealDateFormat(String oldDate, String targetFormat)
	{
		Date date1 = null;
		DateFormat df2 = null;
		try
		{
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
			Date date = df.parse(oldDate);
			SimpleDateFormat df1 = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.UK);
			date1 = df1.parse(date.toString());
			df2 = new SimpleDateFormat(targetFormat);
		}
		catch (ParseException e)
		{
			
			e.printStackTrace();
		}
		return df2.format(date1);
	}
	
	/**
	  * 格式化Date类型的日期;
	 * @param oldDate:原始日期对象;
	 * @param targetFormat:目标格式，eg:yyyy-MM-dd hh:mm:ss;
	 * @return
	 */
	public static String formatDate(Date oldDate, String targetFormat)
	{
		SimpleDateFormat ft = new SimpleDateFormat(targetFormat);
		return ft.format(oldDate);
	}
	
	/**
	  * 字符串转化为Date类型;
	 * @param timeStr:时间字符串;
	 * @return
	 * @throws ParseException
	 */
	public static Date strToDate(String timeStr)
	{
		SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
		Date parsedDate = null;
		try
		{
			parsedDate = ft.parse(timeStr);
		}
		catch (ParseException e)
		{
			e.printStackTrace();
		}
		return parsedDate;
	}
	
	/**
	 * 解析easyExcel中的时间浮点数未时间格式;
	       * 解析时间 43458.234189814815   解析秒数的例子
	 */
	public static LocalTime getTime(double d)
	{
		//全部转成秒
		int sec = (int) (d * 86400);
		//时
		int s = sec / 3600;
		//分
		int h = sec % 3600 / 60;
		//秒
		int m = sec % 3600 % 60;
		
		String ss = s + "";
		String hh = h + "";
		String mm = m + "";
		
		if (s < 10)
		{
			ss = "0" + ss;
		}
		if (h < 10)
		{
			hh = "0" + hh;
			
		}
		if (m < 10)
		{
			mm = "0" + mm;
		}
		LocalTime time = LocalTime.parse(ss + ":" + hh + ":" + mm);
		return time;
	}
	
	@SuppressWarnings("unused")
	public static void main(String[] args)
	{
		//Date strToDate = strToDate("2019-07-15 15:48:35");
		//43458.234189814815
		TimeUtils.getTime(Double.parseDouble("43458.234189814815"));
	}
}
